using System;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using mfRCF.Core;
using mfRCF.Core.Extensions;

using mfXmlSerializer;

using mfLogger;

using OST.Commands.Extensions;

namespace OST.Commands
{

    /// <summary>
    /// restarts the system, by 'PowerState.RebootDevice(false);'
    /// </summary>
    /// <remarks>references2: http://forums.netduino.com/index.php?/topic/3420-how-to-reboot-netduino-from-code-on-a-socket-exception and
    /// http://forums.netduino.com/index.php?/topic/2729-powerstaterebootdevice-softhard-option/ </remarks>
    public class RebootGETCommand : mfRcfCommand
    {
        #region CTOR

        public RebootGETCommand(string httpPath)
            : base(httpPath, new mfGETMethod())
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// wrapper-property 4 logging
        /// </summary>
        private ILoggerAppender Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }

        #endregion

        #region mfRcfCommand
        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            try
            {
                var rebootDelay = 3000;

                var msg = "reboot-request received... reboot in " + rebootDelay + "ms";
                Logger.Info(msg);
                this.WriteHttpResponseOK(httpContext, msg);

                Thread.Sleep(rebootDelay);
                PowerState.RebootDevice(false);
            }
            catch (Exception ex)
            {
                this.WriteHttpResponseError(httpContext, ex);
            }
        }

        #endregion
    }
}
